using B.S.Data.MES.API.Applications.BaseData.Command.ChanXIanCommand;
using B.S.Domain.BasicData.Factory_modeling.Production_line_management;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.ChanXIanCommandHandlers
{
    /// <summary>
    /// 批量删除产线处理器
    /// </summary>
    public class PLDeleChanXianCommandHandler : IRequestHandler<PLDeleChanXianCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<Production_line_management> _productionLineRepository;

        public PLDeleChanXianCommandHandler(IBaseRepository<Production_line_management> productionLineRepository)
        {
            _productionLineRepository = productionLineRepository;
        }

        public async Task<APIRequst<int>> Handle(PLDeleChanXianCommand request, CancellationToken cancellationToken)
        {
            APIRequst<int> response = new APIRequst<int>();
            response.Code = RequstCode.成功;
            response.Msg = RequstCode.成功.ToString();

            // 查询要删除的产线
            var productionLines = _productionLineRepository.GetAll()
                .Where(pl => request.ProductionlinePKs.Contains(pl.ProductionlinePK))
                .ToList();

            // 执行软删除
            foreach (var productionLine in productionLines)
            {
                productionLine.isDel = true;
            }

            // 批量更新
            await _productionLineRepository.UpdateRange(productionLines);

            return response;
        }
    }
}